package ui.activity.profit.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import dao.TradeRecordItem;
import dao.TradeRecordOutput;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.profit.biz.TradeRecordBiz;
import ui.activity.profit.contract.TradeRecordContract;
import ui.model.TradeRecordBean;

/**
 * Created by chenjian on 19/2/26
 */

public class TradeRecordPresenter implements TradeRecordContract.Presenter {

    TradeRecordContract.View view;
    TradeRecordBiz tradeRecordBiz;
    List<TradeRecordItem> list = new ArrayList<>();

    @Inject
    public TradeRecordPresenter(TradeRecordContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        tradeRecordBiz = (TradeRecordBiz) biz;
    }


    @Override
    public void getInfo(final boolean isRefresh, String userId) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        TradeRecordBean a = new TradeRecordBean();
        a.setCustomId(userId);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(a));
        tradeRecordBiz.getTradeInfo(requestBody, new BaseBiz.Callback<TradeRecordOutput>() {
            @Override
            public void onSuccess(TradeRecordOutput info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(TradeRecordOutput info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

}
